package com.aimosheng.main.views;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.aimosheng.common.utils.WordUtil;
import com.aimosheng.main.R;

/**
 * Created by cxf on 2018/9/22.
 * MainActivity 首页
 */

public class MainHomeViewHolder extends AbsMainHomeParentViewHolder implements View.OnClickListener {

    private MainHomeRecommendViewHolder mRecommendViewHolder;
    private MainHomeNearViewHolder mNearViewHolder;
    private MainHomeNewViewHolder mHomeNewestViewHolder;
    private MainHomeFollowViewHolder mFollowViewHolder;
    private MainHomeUserViewHolder mUserViewHolder;
    private View mBtnFilter;

    private int mCurrentPos = 0;


    public MainHomeViewHolder(Context context, ViewGroup parentView) {
        super(context, parentView);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_main_home;
    }

    @Override
    public void init() {
        setStatusHeight();
        super.init();
        mBtnFilter = findViewById(R.id.btn_filter);
        mBtnFilter.setOnClickListener(this);
    }

    @Override
    protected void loadPageData(int position) {
        if (mViewHolders == null) {
            return;
        }
        mCurrentPos = position;
        AbsMainHomeChildViewHolder vh = mViewHolders[position];
        if (vh == null) {
            if (mViewList != null && position < mViewList.size()) {
                FrameLayout parent = mViewList.get(position);
                if (parent == null) {
                    return;
                }
                if (position == 0) {
                    mRecommendViewHolder = new MainHomeRecommendViewHolder(mContext, parent);
                    vh = mRecommendViewHolder;
                } else if (position == 1) {
                    mNearViewHolder = new MainHomeNearViewHolder(mContext, parent);
                    vh = mNearViewHolder;
                } else if (position == 2) {
                    mHomeNewestViewHolder = new MainHomeNewViewHolder(mContext, parent);
                    vh = mHomeNewestViewHolder;
                } else if (position == 3) {
                    mFollowViewHolder = new MainHomeFollowViewHolder(mContext, parent);
                    vh = mFollowViewHolder;
                } else if (position == 4) {
                    mUserViewHolder = new MainHomeUserViewHolder(mContext, parent);
                    vh = mUserViewHolder;
                }
                if (vh == null) {
                    return;
                }
                mViewHolders[position] = vh;
                vh.addToParent();
                vh.subscribeActivityLifeCycle();
            }
        }
        if (vh != null) {
            vh.loadData();
        }
        if (mBtnFilter != null) {
            if (position == 0 || position == 4) {
                if (mBtnFilter.getVisibility() != View.VISIBLE) {
                    mBtnFilter.setVisibility(View.VISIBLE);
                }
            } else {
                if (mBtnFilter.getVisibility() == View.VISIBLE) {
                    mBtnFilter.setVisibility(View.INVISIBLE);
                }
            }
        }
    }

    @Override
    protected int getPageCount() {
        return 5;
    }

    @Override
    protected String[] getTitles() {
        return new String[]{
                WordUtil.getString(R.string.recommend),
                WordUtil.getString(R.string.near),
                WordUtil.getString(R.string.newest),
                WordUtil.getString(R.string.follow),
                WordUtil.getString(R.string.home_user)
        };
    }


    @Override
    public void onClick(View v) {
        if (mCurrentPos == 0) {
            if (mRecommendViewHolder != null) {
                mRecommendViewHolder.onFilterClick();
            }
        } else if (mCurrentPos == 4) {
            if (mUserViewHolder != null) {
                mUserViewHolder.filterUser();
            }
        }
    }
}
